<?php

class m130915_121108_create_tbl_article_comment_category extends CDbMigration
{
    public function up()
    {
        $this->createTable('tbl_article', array(
            'id' => 'pk',
            'mid' => 'int(5) NOT NULL',
            'title'=>'string NOT NULL',
            'content' => 'text NOT NULL',
            'description' => 'text NOT NULL',
            'rating'=> 'int(11) DEFAULT NULL',
            'image'=>'string NOT NULL',
            'create_time' => 'datetime DEFAULT NULL',
            'update_time' => 'datetime DEFAULT NULL',
            'status'=>'boolean DEFAULT 0',
            'category_id'=>'int(11) DEFAULT NULL',
            'create_user_id'=>'int(11) DEFAULT NULL',
            'update_user_id'=>'int(11) DEFAULT NULL',
            'tags'=>'text NOT NULL',

        ), 'ENGINE=InnoDB');
        $this->createTable('tbl_comment', array(
            'id' => 'pk',
            'content' => 'text NOT NULL',
            'article_id' => 'int(11) NOT NULL',
            'create_time' => 'datetime DEFAULT NULL',
            'status'=>'boolean DEFAULT 0',
            'create_user_id' => 'int(11) DEFAULT NULL',
            'update_user_id' => 'int(11) DEFAULT NULL',
            'email'=>'string NOT NULL',
        ), 'ENGINE=InnoDB');
        $this->createTable('tbl_category', array(
            'id' => 'pk',
            'name'=>'string NOT NULL',
            'create_time' => 'datetime DEFAULT NULL',
            'update_time' => 'datetime DEFAULT NULL',
            'tags'=>'text NOT NULL',
            'status'=>'int(1)DEFAULT 0',
            'access'=>'int(1) DEFAULT 0',
            'create_user_id' => 'int(11) DEFAULT NULL',
            'update_user_id' => 'int(11) DEFAULT NULL',
        ), 'ENGINE=InnoDB');

        $this->createTable('tbl_rating', array(
            'id'=>'pk',
            'article_id'=>'int(11) NOT NULL',
            'create_user_id'=>'int(11) NOT NULL',
            'value'=>'tinyint(3) NOT NULL',
        ),'ENGINE=InnoDB');

        $this->addForeignKey('fk_article_comment','tbl_comment','article_id','tbl_article','id','CASCADE','RESTRICT');
        $this->addForeignKey('fk_article_create_user','tbl_article','create_user_id','tbl_user','id','RESTRICT','RESTRICT');
        $this->addForeignKey('fk_article_update_user','tbl_article','update_user_id','tbl_user','id','RESTRICT','RESTRICT');

        $this->addForeignKey('fk_article_menu','tbl_article','mid','tbl_menu','id','RESTRICT','RESTRICT');

        $this->addForeignKey('fk_article_update_user','tbl_article','update_user_id','tbl_user','id','RESTRICT','RESTRICT');

        $this->addForeignKey('fk_comment_create_user_id','tbl_comment','create_user_id','tbl_user','id','RESTRICT','RESTRICT');
        $this->addForeignKey('fk_comment_update_user','tbl_comment','update_user_id','tbl_user','id','RESTRICT','RESTRICT');

        $this->addForeignKey('fk_article_category','tbl_article','category_id','tbl_category','id','RESTRICT','RESTRICT');
        $this->addForeignKey('fk_category_create_user','tbl_category','create_user_id','tbl_user','id','RESTRICT','RESTRICT');
        $this->addForeignKey('fk_category_update_user','tbl_category','update_user_id','tbl_user','id','RESTRICT','RESTRICT');

        $this->addForeignKey('fk_rating_user','tbl_rating','create_user_id','tbl_user','id','CASCADE','CASCADE');
        $this->addForeignKey('fk_rating_article','tbl_rating','article_id','tbl_article','id','CASCADE','CASCADE');
    }

    public function down()
    {
        $this->dropForeignKey('fk_article_comment', 'tbl_article');
        $this->dropForeignKey('fk_article_create_user', 'tbl_article');
        $this->dropForeignKey('fk_article_update_user', 'tbl_article');

        $this->dropForeignKey('fk_comment_create_user_id', 'tbl_comment');
        $this->dropForeignKey('fk_comment_update_user', 'tbl_comment');

        $this->dropForeignKey('fk_category_article', 'tbl_category');
        $this->dropForeignKey('fk_category_create_user', 'tbl_category');
        $this->dropForeignKey('fk_category_update_user', 'tbl_category');

        $this->dropForeignKey('fk_rating_user', 'tbl_rating');
        $this->dropForeignKey('fk_rating_article', 'tbl_rating');

        $this->dropTable('tbl_article');
        $this->dropTable('tbl_comment');
        $this->dropTable('tbl_category');
        $this->dropTable('tbl_rating');
    }
    /*
    // Use safeUp/safeDown to do migration with transaction
    public function safeUp()
    {
    }

    public function safeDown()
    {
    }
    */
}